package com.gitee.xqxyxchy.provider;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Optional;

import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;

@Controller
@ApiIgnore
@RequestMapping("/swagger-resources")
public class GatewayApiResourceController {

	@Autowired(required = false)
	private SecurityConfiguration securityConfiguration;
	@Autowired(required = false)
	private UiConfiguration uiConfiguration;

	private final SwaggerResourcesProvider swaggerResources;

	@Autowired
	public GatewayApiResourceController(SwaggerResourcesProvider swaggerResources) {
		this.swaggerResources = swaggerResources;
	}

	@RequestMapping(value = "/configuration/security")
	@ResponseBody
	public ResponseEntity<SecurityConfiguration> securityConfiguration() {
		return new ResponseEntity<SecurityConfiguration>(
				Optional.fromNullable(securityConfiguration).or(SecurityConfigurationBuilder.builder().build()),
				HttpStatus.OK);
	}

	@RequestMapping(value = "/configuration/ui")
	@ResponseBody
	public ResponseEntity<UiConfiguration> uiConfiguration() {
		return new ResponseEntity<UiConfiguration>(
				Optional.fromNullable(uiConfiguration).or(UiConfigurationBuilder.builder().build()), HttpStatus.OK);
	}

	@RequestMapping
	@ResponseBody
	public ResponseEntity<List<SwaggerResource>> swaggerResources() {
		return new ResponseEntity<List<SwaggerResource>>(swaggerResources.get(), HttpStatus.OK);
	}

}
